import os

import logging

from tornado.escape import json_decode, json_encode, utf8

import datetime
import cv2
import base64
from TrashDetector import TrashDetector, dection_main



class ControllerExample(object):
    # 控制层业务举例

    def __init__(self, data):
        self.test_data = data

    def do_add(self):
        print("进入函数")
        '''
        --image   检测图片路径
        --model_dir   模型文件路径
        --label_path  名称对应文件
        '''
        # self.set_header("Access-Control-Allow-Origin", "*")
        # self.set_header("Access-Control-Allow-Headers", "x-requested-with")
        # self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
        # get方式传入三个参数
        image = self.test_data.get('image')
        lt = self.test_data.get('lt')
        rt = self.test_data.get('rt')
        lb = self.test_data.get('lb')
        rb = self.test_data.get('rb')
        # logging.info(self.test_data)
        (filename, extension) = os.path.splitext(image)
        if image == "":
            # 日志方式输出
            logging.info('Please enter the path of image you need to detect.')
            print('Please enter the path of image you need to detect.')
            result = {
                "msg": "Please enter the path of image you need to detect",
                "code": 0
            }
            self.test_data = result
        else:
            # model = TrashDetector()
            # class_name, num_detect, loc, img = model.detectByBase64(image)

            class_name, num_detect, loc, img = dection_main(lt, rt, lb, rb, image)
            curr_time = datetime.datetime.now()
            time_str = datetime.datetime.strftime(curr_time, '%Y-%m-%d %H:%M:%S')
            # print(time_str)
            # print(class_name)
            # print(num_detect)
            # print(loc)
            # print(img)

            # cv2.imshow("", img)
            # cv2.waitKey()

            data = cv2.imencode('.jpg', img)[1]

            image_bytes = data.tobytes()
            image_base4 = base64.b64encode(image_bytes).decode('utf8')

            logging.info('图片分析后结果:')
            if num_detect > 0:
                result = {
                    "class_name": class_name,
                    "num_detect": num_detect,
                    "location": loc,
                    "img": str(image_base4),
                    "msg": '检测到垃圾数:' + str(num_detect) + '个',
                    "code": 1
                }
                logging.info('检测到垃圾数共有:' + str(num_detect))
                logging.info('垃圾类别:' + str(result["class_name"]))
                # 图片保存格式
                logging.info('站点名' + time_str + '.jpg')
                self.test_data = result
            elif num_detect == 0:
                logging.info('此站点现在没有垃圾！')
                result = {
                    "class_name": class_name,
                    "num_detect": num_detect,
                    "location": loc,
                    "img": str(image_base4),
                    "msg": '此站点现在没有垃圾！',
                    "code": 1
                }
                self.test_data = result
            else:
                logging.info('光线过暗或过亮！')
                result = {
                    "class_name": class_name,
                    "num_detect": num_detect,
                    "location": loc,
                    "img": str(image_base4),
                    "msg": '光线过暗或过亮！',
                    "code": 1
                }
                self.test_data = result
        return self.test_data
